package weaver.workflow.workflow;

import weaver.conn.RecordSet;
import weaver.conn.RecordSetDataSource;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.servicefiles.DataSourceXML;
import weaver.systeminfo.SystemEnv;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * 工作流程非数据库数据源分页处理
 *
 * @author wuser12
 */
public class WfDataSource {

    /**
     * 工作流程表
     *
     * @param user
     * @param otherparams
     * @return
     */
    public List<Map<String, String>> getWorkflowTable(User user,
                                                      Map<String, String> otherparams, HttpServletRequest request,
                                                      HttpServletResponse response) {
        List<Map<String, String>> data = new ArrayList<Map<String, String>>();
        RecordSet rs = new RecordSet();
        RecordSet rs1 = new RecordSet();
        int modetype = Util.getIntValue(otherparams.get("modetype"), -1);
        String tablename = Util.null2String(otherparams.get("tablename"));
        //联想搜索
        String forname = Util.null2String(request.getParameter("q"));
        if ("".equals(tablename) && !"".equals(forname)) {
            tablename = forname;
        }
        String sql = "";
        if (modetype == 0 || modetype == -1) {
            sql = "select id,formname from workflow_formbase where id in (select distinct formid from workflow_base where isvalid=1)";
            if (!tablename.equals(""))
                sql += " and formname like '%" + tablename + "%'";
            rs.executeSql(sql);
            while (rs.next()) {
                Map<String, String> d = new HashMap<String, String>();
                d.put("id", rs.getString("id"));
                d.put("tablelabelname", rs.getString("formname") + "(" + SystemEnv.getHtmlLabelName(21778, user.getLanguage()) + ")");
                d.put("belongsTo", SystemEnv.getHtmlLabelName(2118, user.getLanguage()));
                d.put("other1", "workflow_form");
                d.put("other2", rs.getString("id"));
                d.put("other3", "-1");
                data.add(d);
                int tempindex = 0;
                rs1.execute("select distinct groupId from workflow_formfield where formid="
                        + rs.getString("id") + " and isdetail=1 order by groupId");
                while (rs1.next()) {
                    tempindex++;
                    Map<String, String> d1 = new HashMap<String, String>();
                    d1.put("id", rs.getString("id") + "_" + rs1.getString("groupId"));
                    d1.put("tablelabelname", rs.getString("formname") + "(" + SystemEnv.getHtmlLabelName(19325, user.getLanguage()) + tempindex + ")");
                    d1.put("belongsTo", SystemEnv.getHtmlLabelName(2118, user.getLanguage()));
                    d1.put("other1", "workflow_formdetail");
                    d1.put("other2", rs.getString("id"));
                    d1.put("other3", rs1.getString("groupId"));
                    data.add(d1);
                }
            }
        }
        if (modetype == 0 || modetype == -1) {
            sql = "select tablename,id,namelabel,detailtablename from workflow_bill where not exists (select 1 from ModeFormExtend where formid=id and isvirtualform=1) order by id";
            rs.executeSql(sql);
            while (rs.next()) {
                String lablename = SystemEnv.getHtmlLabelName(rs.getInt("namelabel"), user.getLanguage());
                // 判断取得的标签表名是否为null值，修正当输入查询条件数据库表名的时候标签表名为null的时候查询出错。
                if (lablename == null) {
                    continue;
                }
                if (!tablename.equals(""))
                    if (lablename.indexOf(tablename) < 0)
                        continue;
                Map<String, String> d = new HashMap<String, String>();
                d.put("id", "0");
                d.put("tablelabelname", lablename + "(" + SystemEnv.getHtmlLabelName(21778, user.getLanguage()) + ")");
                d.put("belongsTo", SystemEnv.getHtmlLabelName(2118, user.getLanguage()));
                d.put("other1", rs.getString(1));
                d.put("other2", rs.getString("id"));
                d.put("other3", "-1");
                data.add(d);
                String detailtablename = Util.null2String(rs.getString("detailtablename"));
                String billid = Util.null2String(rs.getString("id"));
                rs1.execute("select tablename,orderid from Workflow_billdetailtable where tablename='" + detailtablename + "'");
                if (!detailtablename.equals("") && !rs1.next()) {
                    Map<String, String> d1 = new HashMap<String, String>();
                    d1.put("id", "0");
                    d1.put("tablelabelname", lablename + "(" + SystemEnv.getHtmlLabelName(19325, user.getLanguage()) + "1)");
                    d1.put("belongsTo", SystemEnv.getHtmlLabelName(2118, user.getLanguage()));
                    d1.put("other1", detailtablename);
                    d1.put("other2", rs.getString("id"));
                    d1.put("other3", rs1.getString("orderid"));
                    data.add(d1);
                }
                rs1.execute("select tablename,orderid from Workflow_billdetailtable where billid="
                        + rs.getString("id") + " order by orderid");
                int detailIndex = 0;
                while (rs1.next()) {
                    detailIndex++;
                    Map<String, String> d1 = new HashMap<String, String>();
                    d1.put("id", "0");
                    d1.put("tablelabelname", lablename + "(" + SystemEnv.getHtmlLabelName(19325, user.getLanguage()) + detailIndex + ")");
                    d1.put("belongsTo", SystemEnv.getHtmlLabelName(2118, user.getLanguage()));
                    d1.put("other1", rs1.getString(1));
                    d1.put("other2", rs.getString("id"));
                    d1.put("other3", rs1.getString("orderid"));
                    data.add(d1);
                }
            }
        }
        if (user.getLanguage() == 7 || user.getLanguage() == 9){
            sql = "select tablename,tabledesc,modetype from Sys_tabledict";
        } else {
            sql = "select tablename,tabledescen,modetype from Sys_tabledict";
        }
        sql += " where 1=1 ";
        if (modetype != -1) {
            sql += " and modetype=" + modetype;
        }
        if (!tablename.equals("")) {
            if (user.getLanguage() == 7 || user.getLanguage() == 9){
                sql += " and tabledesc like '%" + tablename + "%'";
            } else {
                sql += " and tabledescen like '%" + tablename + "%'";
            }
        }
        sql += " order by modetype";
        rs.executeSql(sql);
        while (rs.next()) {
            String tempmodetype = Util.null2String(rs.getString("modetype"));
            String modename = "";
            if (tempmodetype.equals("1"))
                modename = SystemEnv.getHtmlLabelName(179, user.getLanguage());
            if (tempmodetype.equals("2"))
                modename = SystemEnv.getHtmlLabelName(2115, user.getLanguage());
            if (tempmodetype.equals("3"))
                modename = SystemEnv.getHtmlLabelName(2113, user.getLanguage());
            if (tempmodetype.equals("4"))
                modename = SystemEnv.getHtmlLabelName(2114, user.getLanguage());
            if (tempmodetype.equals("5"))
                modename = SystemEnv.getHtmlLabelName(2116, user.getLanguage());
            if (tempmodetype.equals("6"))
                modename = SystemEnv.getHtmlLabelName(2117, user.getLanguage());
            if (tempmodetype.equals("7"))
                modename = SystemEnv.getHtmlLabelName(18442, user.getLanguage());
            if (tempmodetype.equals("8"))
                modename = SystemEnv.getHtmlLabelName(17629, user.getLanguage());
            Map<String, String> d = new HashMap<String, String>();
            d.put("id", "0");
            d.put("tablelabelname", rs.getString(2) + "(" + SystemEnv.getHtmlLabelName(21778, user.getLanguage()) + ")");
            d.put("belongsTo", modename);
            d.put("other1", rs.getString(1));
            d.put("other2", "-1");
            d.put("other3", "-1");
            data.add(d);
        }
        return data;
    }

    public List<Map<String, String>> getWorkflowTableForE9(User user,
                                                           Map<String, String> otherparams, HttpServletRequest request,
                                                           HttpServletResponse response) {

        List<Map<String, String>> list = getWorkflowTable(user, otherparams, request, response);
        for (Map<String, String> stringStringMap : list) {
            stringStringMap.put("id", stringStringMap.get("id") + "?" + Util.getRandom());
        }

        return list;
    }

    public List<Map<String, String>> getWorkflowTableForE92(User user,
                                                            Map<String, String> otherparams, HttpServletRequest request,
                                                            HttpServletResponse response) {

        List<Map<String, String>> data = new ArrayList<Map<String, String>>();
        RecordSet rs = new RecordSet();
        RecordSet rs1 = new RecordSet();
        int modetype = Util.getIntValue(otherparams.get("modetype"), -1);
        int searchflag = Util.getIntValue(otherparams.get("searchflag"), -1);
        String tablename = Util.null2String(otherparams.get("tablename"));
        String sql = "";
        if (modetype == 0 || modetype == -1) {
            sql = "select id,formname from workflow_formbase where id in (select distinct formid from workflow_base where isvalid=1)";
            if (!tablename.equals(""))
                sql += " and formname like '%" + tablename + "%'";
            rs.executeSql(sql);
            while (rs.next()) {
                Map<String, String> d = new HashMap<String, String>();
                d.put("id", "workflow_form");
                d.put("key", "workflow_form");
                d.put("tablelabelname", rs.getString("formname") + "(" + SystemEnv.getHtmlLabelName(21778, user.getLanguage()) + ")");
                d.put("belongsTo", SystemEnv.getHtmlLabelName(2118, user.getLanguage()));
                d.put("other1", "workflow_form");
                d.put("other2", rs.getString("id"));
                d.put("other3", "-1");
                data.add(d);
                int tempindex = 0;
                rs1.execute("select distinct groupId from workflow_formfield where formid="
                        + rs.getString("id") + " and isdetail=1 order by groupId");
                while (rs1.next()) {
                    tempindex++;
                    Map<String, String> d1 = new HashMap<String, String>();
                    d1.put("id", "workflow_formdetail");
                    d1.put("key", "workflow_formdetail");
                    d1.put("tablelabelname", rs.getString("formname") + "(" + SystemEnv.getHtmlLabelName(19325, user.getLanguage()) + tempindex + ")");
                    d1.put("belongsTo", SystemEnv.getHtmlLabelName(2118, user.getLanguage()));
                    d1.put("other1", "workflow_formdetail");
                    d1.put("other2", rs.getString("id"));
                    d1.put("other3", rs1.getString("groupId"));
                    data.add(d1);
                }
            }
        }
        if (modetype == 0 || modetype == -1) {
            if (searchflag == 1) {
                sql = "select tablename,id,namelabel,detailtablename from workflow_bill a " +
                        " left join ModeFormExtend c on a.id=c.formid " +
                        " where  (isvirtualform is null or isvirtualform != 1) " +
                        " order by id";
            } else {
                sql = "select tablename,id,namelabel,detailtablename from workflow_bill order by id";
            }
            rs.executeSql(sql);
            while (rs.next()) {
                String lablename = SystemEnv.getHtmlLabelName(rs.getInt("namelabel"), user.getLanguage());
                // 判断取得的标签表名是否为null值，修正当输入查询条件数据库表名的时候标签表名为null的时候查询出错。
                if (lablename == null) {
                    continue;
                }
                if (!tablename.equals(""))
                    if (lablename.indexOf(tablename) < 0)
                        continue;
                Map<String, String> d = new HashMap<String, String>();
                d.put("id", rs.getString(1));
                d.put("key", rs.getString(1));
                d.put("tablelabelname", lablename + "(" + SystemEnv.getHtmlLabelName(21778, user.getLanguage()) + ")");
                d.put("belongsTo", SystemEnv.getHtmlLabelName(2118, user.getLanguage()));
                d.put("other1", rs.getString(1));
                d.put("other2", rs.getString("id"));
                d.put("other3", "-1");
                data.add(d);
                String detailtablename = Util.null2String(rs.getString("detailtablename"));
                String billid = Util.null2String(rs.getString("id"));
                rs1.execute("select tablename,orderid from Workflow_billdetailtable where tablename='" + detailtablename + "'");
                if (!detailtablename.equals("") && !rs1.next()) {
                    Map<String, String> d1 = new HashMap<String, String>();
                    d1.put("id", detailtablename);
                    d1.put("key", Util.getRandom());
                    d1.put("tablelabelname", lablename + "(" + SystemEnv.getHtmlLabelName(19325, user.getLanguage()) + "1)");
                    d1.put("belongsTo", SystemEnv.getHtmlLabelName(2118, user.getLanguage()));
                    d1.put("other1", detailtablename);
                    d1.put("other2", rs.getString("id"));
                    d1.put("other3", rs1.getString("orderid"));
                    data.add(d1);
                }
                rs1.execute("select tablename,orderid from Workflow_billdetailtable where billid="
                        + rs.getString("id") + " order by orderid");
                int detailIndex = 0;
                while (rs1.next()) {
                    detailIndex++;
                    Map<String, String> d1 = new HashMap<String, String>();
                    d1.put("id", rs1.getString(1));
                    d1.put("key", Util.getRandom());
                    d1.put("tablelabelname", lablename + "(" + SystemEnv.getHtmlLabelName(19325, user.getLanguage()) + detailIndex + ")");
                    d1.put("belongsTo", SystemEnv.getHtmlLabelName(2118, user.getLanguage()));
                    d1.put("other1", rs1.getString(1));
                    d1.put("other2", rs.getString("id"));
                    d1.put("other3", rs1.getString("orderid"));
                    data.add(d1);
                }
            }
        }
        if (user.getLanguage() == 7)
            sql = "select tablename,tabledesc,modetype from Sys_tabledict";
        else if (user.getLanguage() == 8)
            sql = "select tablename,tabledescen,modetype from Sys_tabledict";

        sql += " where 1=1 ";
        if (modetype != -1) {
            sql += " and modetype=" + modetype;
        }
        if (!tablename.equals("")) {
            if (user.getLanguage() == 7)
                sql += " and tabledesc like '%" + tablename + "%'";
            else if (user.getLanguage() == 8)
                sql += " and tabledescen like '%" + tablename + "%'";
        }
        sql += " order by modetype";
        rs.executeSql(sql);
        while (rs.next()) {
            String tempmodetype = Util.null2String(rs.getString("modetype"));
            String modename = "";
            if (tempmodetype.equals("1"))
                modename = SystemEnv.getHtmlLabelName(179, user.getLanguage());
            if (tempmodetype.equals("2"))
                modename = SystemEnv.getHtmlLabelName(2115, user.getLanguage());
            if (tempmodetype.equals("3"))
                modename = SystemEnv.getHtmlLabelName(2113, user.getLanguage());
            if (tempmodetype.equals("4"))
                modename = SystemEnv.getHtmlLabelName(2114, user.getLanguage());
            if (tempmodetype.equals("5"))
                modename = SystemEnv.getHtmlLabelName(2116, user.getLanguage());
            if (tempmodetype.equals("6"))
                modename = SystemEnv.getHtmlLabelName(2117, user.getLanguage());
            if (tempmodetype.equals("7"))
                modename = SystemEnv.getHtmlLabelName(18442, user.getLanguage());
            Map<String, String> d = new HashMap<String, String>();
            d.put("id", rs.getString(1));
            d.put("key", Util.getRandom());
            d.put("tablelabelname", rs.getString(2) + "(" + SystemEnv.getHtmlLabelName(21778, user.getLanguage()) + ")");
            d.put("belongsTo", modename);
            d.put("other1", rs.getString(1));
            d.put("other2", "-1");
            d.put("other3", "-1");
            data.add(d);
        }

        for (Map<String, String> datum : data) {
            datum.put("randomFieldId", Util.getRandom());
        }

        return data;
    }

    /**
     * 表字段
     *
     * @param user
     * @param otherparams
     * @param request
     * @param response
     * @return
     */
    public List<Map<String, String>> getWorkflowTableField(User user,
                                                           Map<String, String> otherparams, HttpServletRequest request,
                                                           HttpServletResponse response) {
        List<Map<String, String>> data = new ArrayList<Map<String, String>>();
        String searchtablename = Util.null2String(otherparams.get("searchtablename"));
        String searchfieldname = Util.null2String(otherparams.get("searchfieldname"));
        String datasourceid = Util.null2String(otherparams.get("datasourceid"));
        if (searchtablename != null) {
            searchtablename = searchtablename.trim();
        }
        if (searchfieldname != null) {
            searchfieldname = searchfieldname.trim();
        }
        if (datasourceid != null) {
            datasourceid = datasourceid.trim();
        }
        String tablenames = otherparams.get("tablenames");
        ArrayList tablenamesArray = Util.TokenizerString(tablenames, ",");
        String sql = "";
        int i = 0;
        RecordSetDataSource rsd = new RecordSetDataSource();
        RecordSet rs3 = new RecordSet();
        RecordSet rs1 = new RecordSet();
        RecordSet rs = new RecordSet();
        for (int j = 0; j < tablenamesArray.size(); j++) {
            String tempS = (String) tablenamesArray.get(j);
            String formid = "";
            String tablename = "";
            String tablelable = "";
            String[] tempsarry = tempS.split("~");
            formid = tempsarry[0];//tempS.substring(0,tempS.indexOf("~"));
            tablename = tempsarry[1]; //tempS.substring(tempS.indexOf("~")+1,tempS.length());
            String diyname = "";
            String formidspan = "";
            String tabfix = "";
            if (tempsarry.length == 4) {
                diyname = tempsarry[2];
                formidspan = tempsarry[3];
            }
            if (datasourceid != null && !datasourceid.equals("")) {
                if (!searchtablename.equalsIgnoreCase("") && !searchtablename.equalsIgnoreCase(tablename)) {
                    continue;
                }
                ArrayList allcolnums = rsd.getAllColumns(datasourceid, tablename);
                for (int m = 0; m < allcolnums.size(); m++) {
                    String fieldname = Util.null2String((String) allcolnums.get(m));
                    if (!"".equals(searchfieldname) && fieldname.toUpperCase().indexOf(searchfieldname.toUpperCase()) == -1) {
                        continue;
                    }
                    Map<String, String> d = new HashMap<String, String>();
                    d.put("fieldname", fieldname);
                    d.put("fieldlabel", fieldname);
                    d.put("tablename", tablename);
                    d.put("tablelabel", tablename);
                    if (diyname.equals("-")) {
                        tabfix = tablename + ".";
                    } else {
                        tabfix = diyname + ".";
                    }
                    d.put("tabfix", tabfix);
                    data.add(d);
                }

            } else {
                if (diyname.equals("-")) {
                    if (formidspan.equals("") || formidspan.equalsIgnoreCase("null")) {
                        tabfix = "";
                    } else {
                        tabfix = formidspan + ".";
                    }
                } else {
                    tabfix = diyname + ".";
                }
                if (!"".equals(formid) && !"0".equals(formid)) {// 旧表单
                    int groupId = 0;
                    if (tablename.equals("workflow_form"))
                        sql = "select fieldid from workflow_formfield where isdetail is null and formid=" + formid;
                    if (tablename.equals("workflow_formdetail")) {
                        groupId = Util.getIntValue(formid.substring(formid.indexOf("_") + 1, formid.length()), 0);
                        formid = formid.substring(0, formid.indexOf("_"));
                        sql = "select fieldid from workflow_formfield where groupId="
                                + groupId + " and isdetail=1 and formid=" + formid;
                    }
                    rs3.executeSql("select formname from workflow_formbase where id=" + formid);
                    if (rs3.next())
                        tablelable = rs3.getString("formname");
                    if (tablename.equals("workflow_formdetail"))
                        tablelable += "(" + SystemEnv.getHtmlLabelName(19325, user.getLanguage()) + (groupId + 1) + ")";
                    else
                        tablelable += "(" + SystemEnv.getHtmlLabelName(21778, user.getLanguage()) + ")";

                    if (!searchtablename.equals("") && !searchtablename.equals(formid))
                        sql += " and 1=2";
                    rs3.executeSql(sql);
                    boolean firstflag = true;
                    while (rs3.next()) {
                        String fieldid = rs3.getString("fieldid");
                        rs.executeSql("select fieldid,fieldlable from workflow_fieldlable where langurageid="
                                + user.getLanguage() + " and formid=" + formid + " and fieldid=" + fieldid);
                        rs.next();
                        if (!searchfieldname.equals("") && rs.getString("fieldlable").indexOf(searchfieldname) < 0)
                            continue;
                        if (firstflag && !tablename.equals("workflow_formdetail") && "".equals(searchfieldname)) {
                            firstflag = false;
                            Map<String, String> d = new HashMap<String, String>();
                            d.put("fieldname", "requestid");
                            d.put("fieldlabel", SystemEnv.getHtmlLabelName(648, user.getLanguage()) + "ID");
                            d.put("tablename", tablename);
                            d.put("tablelabel", tablelable);
                            d.put("tabfix", tabfix);
                            data.add(d);
                        }
                        String tempSql0 = "";
                        if (tablename.equals("workflow_formdetail"))
                            tempSql0 = "select fieldname from workflow_formdictdetail where id=" + fieldid;
                        else
                            tempSql0 = "select fieldname from workflow_formdict where id=" + fieldid;
                        rs1.executeSql(tempSql0);
                        Map<String, String> d = new HashMap<String, String>();
                        if (rs1.next()) {
                            d.put("fieldname", rs1.getString("fieldname"));
                        }
                        d.put("fieldlabel", rs.getString("fieldlable"));
                        d.put("tablename", tablename);
                        d.put("tablelabel", tablelable);
                        d.put("tabfix", tabfix);
                        data.add(d);
                    }
                } else {
                    if (!searchtablename.equals("") && !tablename.equals(searchtablename))
                        continue;
                    else if (!searchtablename.equals("") && tablename.equals(searchtablename))
                        tablename = searchtablename;

                    rs3.executeSql("select id,namelabel from workflow_bill where tablename='" + tablename + "'");// 新表单,单据主字段
                    boolean firstflag = true;
                    if (rs3.next()) {
                        String billid = rs3.getString("id");
                        tablelable = SystemEnv.getHtmlLabelName(rs3.getInt("namelabel"), user.getLanguage())
                                + "(" + SystemEnv.getHtmlLabelName(21778, user.getLanguage()) + ")";
                        rs.executeSql("select fieldname,fieldlabel from workflow_billfield where viewtype=0 and billid=" + billid);
                        while (rs.next()) {
                            String fieldname = rs.getString("fieldname");
                            String fieldlabel = SystemEnv.getHtmlLabelName(rs.getInt("fieldlabel"), user.getLanguage());
                            if (!searchfieldname.equals("") && fieldlabel.indexOf(searchfieldname) < 0)
                                continue;
                            if (firstflag) {
                                firstflag = false;

                                Map<String, String> d = new HashMap<String, String>();
                                d.put("fieldname", "id");
                                d.put("fieldlabel", ""+weaver.systeminfo.SystemEnv.getHtmlLabelName(563,weaver.general.ThreadVarLanguage.getLang())+"ID");
                                d.put("tablename", tablename);
                                d.put("tablelabel", tablelable);
                                d.put("tabfix", tabfix);
                                data.add(d);

                                d = new HashMap<String, String>();
                                d.put("fieldname", "requestid");
                                d.put("fieldlabel", SystemEnv.getHtmlLabelName(648, user.getLanguage()) + "ID");
                                d.put("tablename", tablename);
                                d.put("tablelabel", tablelable);
                                d.put("tabfix", tabfix);
                                data.add(d);
                            }
                            Map<String, String> d = new HashMap<String, String>();
                            d.put("fieldname", fieldname);
                            d.put("fieldlabel", fieldlabel);
                            d.put("tablename", tablename);
                            d.put("tablelabel", tablelable);
                            d.put("tabfix", tabfix);
                            data.add(d);
                        }
                    }
                    boolean dtfirstflag = true;
                    rs3.executeSql("select id,namelabel from workflow_bill where detailtablename='" + tablename + "'");// 单据明细字段1
                    rs.executeSql("select tablename from Workflow_billdetailtable where tablename='" + tablename + "'");
                    if (rs3.next() && !rs.next()) {

                        dtfirstflag = true;
                        String billid = rs3.getString("id");
                        tablelable = SystemEnv.getHtmlLabelName(rs3.getInt("namelabel"), user.getLanguage())
                                + "(" + SystemEnv.getHtmlLabelName(19325, user.getLanguage()) + "1)";
                        rs.executeSql("select fieldname,fieldlabel from workflow_billfield where viewtype=1 and billid=" + billid);
                        while (rs.next()) {
                            String fieldname = rs.getString("fieldname");
                            String fieldlabel = SystemEnv.getHtmlLabelName(rs.getInt("fieldlabel"), user.getLanguage());
                            if (!searchfieldname.equals("") && fieldlabel.indexOf(searchfieldname) < 0)
                                continue;

                            if (dtfirstflag) {
                                dtfirstflag = false;

                                Map<String, String> d = new HashMap<String, String>();
                                d.put("fieldname", "ID");
                                d.put("fieldlabel", SystemEnv.getHtmlLabelName(81287, Util.getIntValue("" + user.getLanguage(), 7)));
                                d.put("tablename", tablename);
                                d.put("tablelabel", tablelable);
                                d.put("tabfix", tabfix);
                                data.add(d);
                            }

                            Map<String, String> d = new HashMap<String, String>();
                            d.put("fieldname", fieldname);
                            d.put("fieldlabel", fieldlabel);
                            d.put("tablename", tablename);
                            d.put("tablelabel", tablelable);
                            d.put("tabfix", tabfix);
                            data.add(d);
                        }
                    }
                    rs3.executeSql("select billid from Workflow_billdetailtable where tablename='" + tablename + "'");// 新表单,单据明细字段2
                    if (rs3.next()) {
                        String billid = rs3.getString("billid");
                        rs3.executeSql("select namelabel from workflow_bill where id=" + billid);
                        if (rs3.next())
                            tablelable = SystemEnv.getHtmlLabelName(rs3.getInt("namelabel"), user.getLanguage());

                        rs3.executeSql("select tablename from Workflow_billdetailtable where billid="
                                + billid + " order by orderid");
                        int detailIndex = 0;
                        while (rs3.next()) {
                            detailIndex++;
                            String detailtablename = Util.null2String(rs3.getString("tablename"));
                            if (detailtablename.equals(tablename)) {
                                tablelable = tablelable + "(" + SystemEnv.getHtmlLabelName(19325, user.getLanguage()) + detailIndex + ")";
                            }
                        }

                        dtfirstflag = true;
                        rs.executeSql("select fieldname,fieldlabel from workflow_billfield where viewtype=1 and detailtable='"
                                + tablename + "' and billid=" + billid);
                        while (rs.next()) {
                            String fieldname = rs.getString("fieldname");
                            String fieldlabel = SystemEnv.getHtmlLabelName(rs.getInt("fieldlabel"), user.getLanguage());
                            if (!searchfieldname.equals("") && fieldlabel.indexOf(searchfieldname) < 0)
                                continue;

                            if (dtfirstflag) {
                                dtfirstflag = false;

                                Map<String, String> d = new HashMap<String, String>();
                                d.put("fieldname", "ID");
                                d.put("fieldlabel", SystemEnv.getHtmlLabelName(81287, Util.getIntValue("" + user.getLanguage(), 7)));
                                d.put("tablename", tablename);
                                d.put("tablelabel", tablelable);
                                d.put("tabfix", tabfix);
                                data.add(d);
                            }
                            Map<String, String> d = new HashMap<String, String>();
                            d.put("fieldname", fieldname);
                            d.put("fieldlabel", fieldlabel);
                            d.put("tablename", tablename);
                            d.put("tablelabel", tablelable);
                            d.put("tabfix", tabfix);
                            data.add(d);
                        }
                    }
                    rs3.executeSql("select id,tabledesc,tabledescen from Sys_tabledict where tablename='"
                            + tablename + "'");// 其它模块
                    if (rs3.next()) {
                        String tabledictid = rs3.getString("id");
                        if (user.getLanguage() == 7)
                            tablelable = rs3.getString("tabledesc");
                        if (user.getLanguage() == 8)
                            tablelable = rs3.getString("tabledescen");
                        tablelable += "(" + SystemEnv.getHtmlLabelName(21778, user.getLanguage()) + ")";
                        rs.executeSql("select fieldname,fielddesc,fielddescen from Sys_fielddict where tabledictid="
                                + tabledictid + " order by dsporder");
                        while (rs.next()) {
                            String fieldname = rs.getString("fieldname");
                            String fieldlabel = "";
                            if (user.getLanguage() == 7)
                                fieldlabel = rs.getString("fielddesc");
                            if (user.getLanguage() == 8)
                                fieldlabel = rs.getString("fielddescen");
                            if (!searchfieldname.equals("") && fieldlabel.indexOf(searchfieldname) < 0)
                                continue;
                            Map<String, String> d = new HashMap<String, String>();
                            d.put("fieldname", fieldname);
                            d.put("fieldlabel", fieldlabel);
                            d.put("tablename", tablename);
                            d.put("tablelabel", tablelable);
                            d.put("tabfix", tabfix);
                            data.add(d);
                        }
                    }
                }
            }
        }
        return data;
    }

    public List<Map<String, String>> getWorkflowTableTriggerField(User user,
                                                                  Map<String, String> otherparams, HttpServletRequest request,
                                                                  HttpServletResponse response) {
        List<Map<String, String>> data = new ArrayList<Map<String, String>>();
        int wfid = Util.getIntValue(otherparams.get("wfid"), 0);
        String htmltype = Util.null2String(otherparams.get("htmltype"));
        String type = Util.null2String(otherparams.get("type"));
        String fielddbtype = Util.null2String(otherparams.get("fielddbtype"));
        int tabletype = Util.getIntValue(otherparams.get("tabletype"), -1);
        int isE9 = Util.getIntValue(otherparams.get("ise9"), -1);
        String fieldname = Util.null2String(otherparams.get("fieldname"));
        String sqlwhere = Util.toSqlForSplitPage(otherparams.get("sqlwhere"));
        String isfrom = Util.null2String(otherparams.get("isfrom"));
        RecordSet rs = new RecordSet();
        RecordSet rs1 = new RecordSet();
        RecordSet rs2 = new RecordSet();

        boolean hasSqlwhere = false;
        if ("".equals(sqlwhere) && !"".equals(wfid)) {
            hasSqlwhere = false;
        } else {
            hasSqlwhere = true;
        }
        String sql = "";
        String isbill = "";
        String formid = "";
        rs.executeSql("select formid,isbill from workflow_base where id=" + wfid);
        if (rs.next()) {
            formid = rs.getString("formid");
            isbill = rs.getString("isbill");
        }

        if (hasSqlwhere == false) {
            if (isbill.equals("0")) {// 表单
                String fieldtable = "workflow_formdict";
                int lang=user.getLanguage();
                if (tabletype > 0) fieldtable = "workflow_formdictdetail";
                if(lang!=7){
                    sql = "select a.fieldid, b.fieldlable, a.isdetail, a.fieldorder, '' as description, a.groupid as optionkey,c.fieldhtmltype,c.type from workflow_formfield a, workflow_fieldlable b, " + fieldtable + " c "
                            + " where a.formid=b.formid and a.fieldid=b.fieldid AND a.fieldid = c.id and a.formid="
                            + formid + " and (  b.fieldlable !='' or b.FIELDLABLE is not null) and b.langurageid = " + lang;

                    RecordSet rsz=new RecordSet();
                    rsz.executeQuery(sql);
                    if(!rsz.next()){
                        lang=7;
                    }
                }

                sql = "select a.fieldid, b.fieldlable, a.isdetail, a.fieldorder, '' as description, a.groupid as optionkey,c.fieldhtmltype,c.type from workflow_formfield a, workflow_fieldlable b, " + fieldtable + " c "
                        + " where a.formid=b.formid and a.fieldid=b.fieldid AND a.fieldid = c.id and a.formid="
                        + formid + " and b.langurageid = " + lang;
                if (tabletype == 0)
                    sql += " and a.isdetail is null ";
                if (tabletype > 0)
                    sql += " and a.isdetail=1 ";


                if (!fieldname.equals(""))
                    sql += " and b.fieldlable like '%" + fieldname + "%' ";

                if (isE9 == 1) {
                    sql+= " UNION select a.fieldid, b.fieldlable, a.isdetail, a.fieldorder, '' as description, a.groupid as optionkey,c.fieldhtmltype,c.type from workflow_formfield a, workflow_fieldlable b,  workflow_formdictdetail c "
                            + " where a.formid=b.formid and a.fieldid=b.fieldid AND a.fieldid = c.id and a.formid="
                            + formid + " and b.langurageid = " + lang;
                    if (tabletype == 0)
                        sql += " and a.isdetail is null ";
                    if (tabletype > 0)
                        sql += " and a.isdetail=1 ";
                    if (!fieldname.equals(""))
                        sql += " and b.fieldlable like '%" + fieldname + "%' ";

                    if (rs.getDBType().equals("oracle")) {
                        sql += " order by isdetail desc,optionkey asc,fieldorder asc ";
                    } else {
                        sql += " order by isdetail,optionkey,fieldorder ";
                    }
                }else{
                    if (rs.getDBType().equals("oracle")) {
                        sql += " order by a.isdetail desc,optionkey asc,a.fieldorder asc ";
                    } else {
                        sql += " order by a.isdetail,optionkey,a.fieldorder ";
                    }
                }



            } else if (isbill.equals("1")) {// 单据
                sql = "select id as fieldid,fieldlabel,viewtype as isdetail,dsporder as fieldorder, '' as description, detailtable as optionkey,fieldhtmltype,type from workflow_billfield where billid="
                        + formid;
                if (tabletype == 0)
                    sql += " and viewtype=0";
                if (tabletype > 0)
                    sql += " and viewtype=1";
                if (!"".equals(htmltype)) {
                    sql += " and fieldhtmltype in(" + htmltype + ")";
                }
                if (!"".equals(type)) {
                    sql += " and type in(" + type + ")";
                }
                if (!"".equals(fielddbtype)) {
                    sql += " and fielddbtype='" + fielddbtype + "' ";
                }


                sql += " order by viewtype,optionkey,dsporder";
            }
        } else {
            isbill = otherparams.get("isbill");
            int isdetail = Util.getIntValue(otherparams.get("isdetail"), 0);
            if ("1".equals(isbill)) {
                if (isdetail == 0) {
                    sql = "select id as fieldid, fieldlabel, 0 as isdetail, 0 as fieldorder, '' as description, '' as optionkey,fieldhtmltype,type from workflow_billfield "
                            + sqlwhere + " and viewtype = 0";
                } else {
                    sql = "select id as fieldid, fieldlabel, 1 as isdetail, 0 as fieldorder, '' as description, '' as optionkey,fieldhtmltype,type from workflow_billfield "
                            + sqlwhere + " and viewtype <> 0";
                }
            } else {
                if (isdetail == 0) {
                    sql = "select id as fieldid, fieldname as fieldlable, 0 as isdetail, 0 as fieldorder, description, '' as optionkey,fieldhtmltype,type from workflow_formdict "
                            + sqlwhere;
                } else {
                    sql = "select id as fieldid, fieldname as fieldlable, 1 as isdetail, 0 as fieldorder, description, '' as optionkey,fieldhtmltype,type from workflow_formdictdetail "
                            + sqlwhere;
                }
            }
            sql += " order by id";
        }
        String mainOption = "";
        String optionsql = "";
        Hashtable detailOption = new Hashtable();
        if (hasSqlwhere == false) {
            if (isbill.equals("0")) {// 表单主字段
                optionsql = "select a.fieldid, b.fieldlable, a.isdetail, a.fieldorder from workflow_formfield a, workflow_fieldlable b "
                        + " where a.isdetail is null and a.formid=b.formid and a.fieldid=b.fieldid and a.formid="
                        + formid + " and b.langurageid = " + user.getLanguage();
                if (rs.getDBType().equals("oracle")) {
                    optionsql += " order by a.isdetail desc,a.fieldorder asc ";
                } else {
                    optionsql += " order by a.isdetail,a.fieldorder ";
                }
            } else if (isbill.equals("1")) {// 单据主字段
                optionsql = "select id,fieldlabel,viewtype,dsporder from workflow_billfield where viewtype=0 and billid="
                        + formid;
                optionsql += " order by viewtype,dsporder";
            }
            rs.executeSql(optionsql);
            while (rs.next()) {
                String tempfieldname = "";
                if (isbill.equals("0"))
                    tempfieldname = rs.getString("fieldlable");
                if (isbill.equals("1"))
                    tempfieldname = SystemEnv.getHtmlLabelName(rs
                            .getInt("fieldlabel"), user.getLanguage());
                mainOption += "<,option value=" + rs.getString(1) + ">"
                        + tempfieldname + "</option,>";
            }

            if (isbill.equals("0")) {
                ArrayList detaigroupidlist = new ArrayList();
                optionsql = "select distinct groupid from workflow_formfield where formid=" + formid;
                rs.executeSql(optionsql);
                while (rs.next()) {
                    String tempgroupid = Util.null2String(rs.getString("groupid"));
                    if (tempgroupid.equals(""))
                        continue;
                    detaigroupidlist.add(tempgroupid);
                }
                for (int j = 0; j < detaigroupidlist.size(); j++) {
                    String tempgroupid = (String) detaigroupidlist.get(j);
                    if (tempgroupid.equals(""))
                        continue;
                    optionsql = "select a.fieldid, b.fieldlable, a.isdetail, a.fieldorder from workflow_formfield a, workflow_fieldlable b "
                            + " where a.isdetail=1 and a.formid=b.formid and a.fieldid=b.fieldid and a.formid="
                            + formid
                            + " and b.langurageid = "
                            + user.getLanguage()
                            + " and groupid="
                            + tempgroupid;
                    if (rs.getDBType().equals("oracle")) {
                        optionsql += " order by a.isdetail desc,a.fieldorder asc ";
                    } else {
                        optionsql += " order by a.isdetail,a.fieldorder ";
                    }
                    rs.executeSql(optionsql);
                    String wfDetailFieldsOptions = "";
                    while (rs.next()) {
                        String tempfieldname = rs.getString("fieldlable");
                        wfDetailFieldsOptions += "<,option value="
                                + rs.getString(1) + ">" + tempfieldname
                                + "</option,>";
                    }
                    detailOption.put(tempgroupid, wfDetailFieldsOptions);
                }
            } else {
                ArrayList detailtablelist = new ArrayList();
                optionsql = "select tablename from Workflow_billdetailtable where billid= ? order by orderid";
                rs.executeQuery(optionsql,formid);
                while (rs.next()) {
                    String tempdetailtable = Util.null2String(rs
                            .getString("tablename"));
                    if (tempdetailtable.equals(""))
                        continue;
                    detailtablelist.add(tempdetailtable);
                }
                for (int j = 0; j < detailtablelist.size(); j++) {
                    String tempdetailtable = Util
                            .null2String((String) detailtablelist.get(j));
                    if (tempdetailtable.equals(""))
                        continue;
                    optionsql = "select id,fieldlabel,viewtype,dsporder from workflow_billfield where viewtype=1 and billid="
                            + formid
                            + " and detailtable='"
                            + tempdetailtable
                            + "'";
                    optionsql += " order by viewtype,dsporder";
                    rs.executeSql(optionsql);
                    String wfDetailFieldsOptions = "";
                    while (rs.next()) {
                        String tempfieldname = SystemEnv.getHtmlLabelName(rs
                                .getInt("fieldlabel"), user.getLanguage());
                        wfDetailFieldsOptions += "<,option value="
                                + rs.getString(1) + ">" + tempfieldname
                                + "</option,>";
                    }
                    detailOption.put(tempdetailtable, wfDetailFieldsOptions);
                }
            }
        }
        Map<String,Integer> groupIdMap=new HashMap<>();
        groupIdMap=getGroupIdMap(isbill,formid,rs2);
        rs.executeSql(sql);
        while (rs.next()) {
            String fieldlablename = "";
            String fieldid = rs.getString("fieldid");
            if (isbill.equals("1")) {// 单据无法将字段名称作为查询条件，在这里进行处理
                fieldlablename = SystemEnv.getHtmlLabelName(rs.getInt("fieldlabel"), user.getLanguage());
                if (!fieldname.equals("") && fieldlablename.indexOf(fieldname) < 0)
                    continue;
            } else {
                fieldlablename = Util.null2String(rs.getString("fieldlable"));
            }
            String description_tmp = Util.null2String(rs.getString("description")).trim();
            if (!"".equals(description_tmp)) {
                fieldlablename = description_tmp;
            }
            int id_tmp = Util.getIntValue(rs.getString("fieldid"), 0);

            String tempoption = "," + mainOption;
            String isdetail = Util.null2String(rs.getString("isdetail"));
            if (isdetail.equals("1")) {
                String optionkey = Util.null2String(rs.getString("optionkey"));
                if (!optionkey.equals(""))
                    tempoption = "," + (String) detailOption.get(optionkey);
            }
            tempoption += ",";
            String tabletypestr = "";
            int detailindex = 0;
            if (isdetail.equals("1")) {
                String tempsql = "";
                if (isbill.equals("0")) {
//                    tempsql = "select groupid+1 from workflow_formfield where isdetail=1  and formid=" + formid + " and fieldid=" + fieldid;
                    tempsql = "select groupid as tablename from workflow_formfield where isdetail=1  and formid=" + formid + " and fieldid=" + fieldid;
                } else {
//                    tempsql = "select t.orderid from workflow_billfield b ,workflow_billdetailtable t where b.viewtype=1 and b.detailtable=t.tablename and b.id=" + fieldid;
                    tempsql = "select t.tablename from workflow_billfield b ,workflow_billdetailtable t where b.viewtype=1 and b.detailtable=t.tablename and b.id=" + fieldid;

                }
                rs1.executeSql(tempsql);
                rs1.next();
//                int groupid = Util.getIntValue(rs1.getString(1));
                String tablename=rs1.getString("tablename");
                int groupid = groupIdMap.get(tablename);//明细表groupid直接从算好的map中取
                if (isfrom.equals("1")) {
                    if (tabletype != groupid && tabletype > 0) {
                        continue;
                    }
                }
                detailindex = groupid;
                //既然是明细  那么detailindex就不能小于1 容错错误数据
                if (detailindex < 1) {
                    detailindex = 1;
                }
                tabletypestr = SystemEnv.getHtmlLabelName(19325, user.getLanguage()) + groupid;
                //tabletypestr = SystemEnv.getHtmlLabelName(19325, user.getLanguage()) + getNewGroupid(formid, groupid);
            } else {
                tabletypestr = SystemEnv.getHtmlLabelName(21778, user.getLanguage());
            }

            Map<String, String> d = new HashMap<String, String>();
            if (!rs.getString("fieldhtmltype").equals("9")) {
                d.put("fieldid", fieldid);
                d.put("fieldname", fieldlablename);
                d.put("htmltype", rs.getString("fieldhtmltype"));
                d.put("type", rs.getString("type"));
                d.put("tabletype", tabletypestr);
                d.put("isdetail", isdetail.equals("1") ? "1" : "0");
                d.put("tempoption", tempoption);
                d.put("detailindex", detailindex + "");
                data.add(d);
            }

        }
        for (Map<String, String> datum : data) {
            datum.put("namefix", datum.get("tabletype") + "." + datum.get("fieldname"));
        }
        return data;
    }

    /**
     * 防止明细表删除，重新计算一次最新的顺序位置，主要用于字段联动
     *
     * @param billid
     * @param groupid
     * @return
     */
    public static int getNewGroupid(String billid, int groupid) {
        RecordSet rs = new RecordSet();
        int newgroupid = 0;
        String sql = "";

        if (rs.getOrgindbtype().toLowerCase().equals("gs")) {
             sql = "SELECT t.rid " +
                     " FROM (select rid, orderid " +
                     " from (SELECT row_number() over(order by orderid) as rid,tablename, orderid " +
                     " FROM Workflow_billdetailtable " +
                     " WHERE billid = " + billid +
                     " order by orderid) t1) t " +
                     " WHERE t.orderid = " + groupid;
        }else if (rs.getDBType().equals("oracle")) {
            sql = " SELECT t.rid FROM (select rownum as rid,orderid from (SELECT tablename, orderid FROM Workflow_billdetailtable  WHERE billid = " + billid + " order by orderid) t1) t WHERE t.orderid=" + groupid;
        }else if(rs.getDBType().equals("mysql")){
            sql="SELECT (@rowNum := @rowNum + 1) as rid, t1.orderid " +
                    "      from (SELECT (@rowNum := 0) as rid,tablename, orderid " +
                    "            FROM Workflow_billdetailtable " +
                    "            WHERE billid = " +billid+
                    "            order by orderid) t1) ";
        } else{
            sql = "SELECT t.rid FROM (SELECT ROW_NUMBER() OVER (ORDER BY ORDERid) AS rid ,tablename,orderid FROM Workflow_billdetailtable WHERE billid=" + billid + " ) t WHERE t.orderid=" + groupid;
        }
        rs.executeSql(sql);
        if(rs.getDBType().equals("mysql")){
            while (rs.next()){
                if(groupid==rs.getInt("orderid")){
                    newgroupid=rs.getInt("rid");
                    break;
                }
            }
            if(newgroupid==0){
                newgroupid = groupid;
            }
        }else{
        if (rs.next()) {
            newgroupid = rs.getInt("rid");
        } else {
            newgroupid = groupid;
        }
        }
        return newgroupid;
    }


    public List<Map<String, String>> getWorkflowTableFieldForE9(User user,
                                                                Map<String, String> otherparams, HttpServletRequest request,
                                                                HttpServletResponse response) {
        List<Map<String, String>> data = new ArrayList<Map<String, String>>();
        String searchtablename = Util.null2String(otherparams.get("searchtablename"));
        String searchfieldname = Util.null2String(otherparams.get("searchfieldname"));
        String datasourceid = Util.null2String(otherparams.get("datasourceid"));
        if (searchtablename != null) {
            searchtablename = searchtablename.trim();
        }
        if (searchfieldname != null) {
            searchfieldname = searchfieldname.trim();
        }
        if (datasourceid != null) {
            datasourceid = datasourceid.trim();
        }
        ArrayList pointArrayList = new DataSourceXML().getPointArrayList();
        if(pointArrayList.indexOf(datasourceid)==-1){
            datasourceid="";
        }

        String tablenames = Util.null2String(otherparams.get("tablenames")).replaceAll("maohaoTransfer",":");
        ArrayList tablenamesArray = Util.TokenizerString(tablenames, ",");
        String sql = "";
        int i = 0;
        RecordSetDataSource rsd = new RecordSetDataSource();
        RecordSet rs3 = new RecordSet();
        RecordSet rs1 = new RecordSet();
        RecordSet rs = new RecordSet();
        for (int j = 0; j < tablenamesArray.size(); j++) {
            String tempS = (String) tablenamesArray.get(j);
            String formid = "";
            String tablename = "";
            String tablelable = "";
            String[] tempsarry = tempS.split("~");
            formid = tempsarry[0];//tempS.substring(0,tempS.indexOf("~"));
            tablename = tempsarry[1]; //tempS.substring(tempS.indexOf("~")+1,tempS.length());
            String diyname = "";
            String formidspan = "";
            String tabfix = "";
            if (tempsarry.length == 4) {
                diyname = tempsarry[2];
                formidspan = tempsarry[3];
            }

            if("localhost".equals(datasourceid)||"".equals(datasourceid)){
                if("".equals(formid)||null==formid){
                    //continue;
                }
            }

            if (datasourceid != null && !datasourceid.equals("")) {
                if (!searchtablename.equalsIgnoreCase("") && !searchtablename.equalsIgnoreCase(tablename)) {
                    continue;
                }
                ArrayList allcolnums = rsd.getAllColumns(datasourceid, tablename);
                for (int m = 0; m < allcolnums.size(); m++) {
                    String fieldname = Util.null2String((String) allcolnums.get(m));
                    if (!"".equals(searchfieldname) && fieldname.toUpperCase().indexOf(searchfieldname.toUpperCase()) == -1) {
                        continue;
                    }
                    Map<String, String> d = new HashMap<String, String>();
                    d.put("fieldname", fieldname);
                    d.put("fieldlabel", fieldname);
                    d.put("tablename", tablename);
                    d.put("tablelabel", tablename);
                    if (diyname.equals("-")) {
                        tabfix = tablename + ".";
                    } else {
                        tabfix = diyname + ".";
                    }
                    d.put("tabfix", tabfix);
                    d.put("name", tabfix + fieldname);

                    d.put("fieldnamefix", tabfix + fieldname);
                    data.add(d);
                }

            } else {
                if (diyname.equals("-")) {
                    if (formidspan.equals("") || formidspan.equalsIgnoreCase("null")) {
                        tabfix = "";
                    } else {
                        tabfix = formidspan + ".";
                    }
                } else {
                    tabfix = diyname + ".";
                }
                if (!"".equals(formid) && !"0".equals(formid)) {// 旧表单
                    int groupId = 0;
                    if (tablename.equals("workflow_form"))
                        sql = "select fieldid from workflow_formfield where isdetail is null and formid=" + formid;
                    if (tablename.equals("workflow_formdetail")) {
                        groupId = Util.getIntValue(formid.substring(formid.indexOf("_") + 1, formid.length()), 0);
                        formid = formid.substring(0, formid.indexOf("_"));
                        sql = "select fieldid from workflow_formfield where groupId="
                                + groupId + " and isdetail=1 and formid=" + formid;
                    }
                    rs3.executeSql("select formname from workflow_formbase where id=" + formid);
                    if (rs3.next())
                        tablelable = rs3.getString("formname");
                    if (tablename.equals("workflow_formdetail"))
                        tablelable += "(" + SystemEnv.getHtmlLabelName(19325, user.getLanguage()) + (groupId + 1) + ")";
                    else
                        tablelable += "(" + SystemEnv.getHtmlLabelName(21778, user.getLanguage()) + ")";

                    if (!searchtablename.equals("") && !searchtablename.equals(formid))
                        sql += " and 1=2";
                    rs3.executeSql(sql);
                    boolean firstflag = true;
                    while (rs3.next()) {
                        String fieldid = rs3.getString("fieldid");
                        rs.executeSql("select fieldid,fieldlable from workflow_fieldlable where langurageid="
                                + user.getLanguage() + " and formid=" + formid + " and fieldid=" + fieldid);
                        rs.next();
                        System.out.println("searchfieldname" + rs.getString("fieldlable"));
                        if (!searchfieldname.equals("") && rs.getString("fieldlable").indexOf(searchfieldname) < 0)
                            continue;
                        if (firstflag && !tablename.equals("workflow_formdetail") && searchfieldname.equals("")) {
                            firstflag = false;
                            Map<String, String> d = new HashMap<String, String>();
                            d.put("fieldname", "requestid");
                            d.put("fieldlabel", SystemEnv.getHtmlLabelName(648, user.getLanguage()) + "ID");
                            d.put("tablename", tablename);
                            d.put("tablelabel", tablelable);
                            d.put("tabfix", tabfix);
                            d.put("name", tabfix + SystemEnv.getHtmlLabelName(648, user.getLanguage()) + "ID");
                            d.put("fieldnamefix", tabfix + "requestid");
                            data.add(d);
                        }
                        String tempSql0 = "";
                        if (tablename.equals("workflow_formdetail"))
                            tempSql0 = "select fieldname from workflow_formdictdetail where id=" + fieldid;
                        else
                            tempSql0 = "select fieldname from workflow_formdict where id=" + fieldid;
                        rs1.executeSql(tempSql0);
                        Map<String, String> d = new HashMap<String, String>();
                        String fieldname = "";

                        if (rs1.next()) {
                            fieldname = rs1.getString("fieldname");
                            d.put("fieldname", fieldname);
                        }
                        d.put("fieldlabel", rs.getString("fieldlable"));
                        d.put("tablename", tablename);
                        d.put("tablelabel", tablelable);
                        d.put("tabfix", tabfix);
                        d.put("name", tabfix + rs.getString("fieldlable"));
                        d.put("fieldnamefix", tabfix + fieldname);
                        data.add(d);
                    }
                } else {
                    if (!searchtablename.equals("") && !tablename.equals(searchtablename))
                        continue;
                    else if (!searchtablename.equals("") && tablename.equals(searchtablename))
                        tablename = searchtablename;

                    rs3.executeSql("select id,namelabel from workflow_bill where tablename='" + tablename + "'");// 新表单,单据主字段
                    boolean firstflag = true;
                    if (rs3.next()) {
                        String billid = rs3.getString("id");
                        tablelable = SystemEnv.getHtmlLabelName(rs3.getInt("namelabel"), user.getLanguage())
                                + "(" + SystemEnv.getHtmlLabelName(21778, user.getLanguage()) + ")";
                        rs.executeSql("select fieldname,fieldlabel from workflow_billfield where viewtype=0 and (fieldhtmltype <> 9 or type <> 1) and billid=" + billid);
                        while (rs.next()) {
                            String fieldname = rs.getString("fieldname");
                            String fieldlabel = SystemEnv.getHtmlLabelName(rs.getInt("fieldlabel"), user.getLanguage());
                            if (!searchfieldname.equals("") && fieldlabel.indexOf(searchfieldname) < 0)
                                continue;
                            if (firstflag && searchfieldname.equals("")) {
                                firstflag = false;

                                Map<String, String> d = new HashMap<String, String>();
                                d.put("fieldname", "id");
                                d.put("fieldlabel", ""+weaver.systeminfo.SystemEnv.getHtmlLabelName(563,weaver.general.ThreadVarLanguage.getLang())+"ID");
                                d.put("tablename", tablename);
                                d.put("tablelabel", tablelable);
                                d.put("tabfix", tabfix);
                                d.put("name", tabfix + ""+weaver.systeminfo.SystemEnv.getHtmlLabelName(563,weaver.general.ThreadVarLanguage.getLang())+"ID");
                                d.put("fieldnamefix", tabfix + "id");
                                data.add(d);

                                d = new HashMap<String, String>();
                                d.put("fieldname", "requestid");
                                d.put("fieldlabel", SystemEnv.getHtmlLabelName(648, user.getLanguage()) + "ID");
                                d.put("tablename", tablename);
                                d.put("tablelabel", tablelable);
                                d.put("tabfix", tabfix);
                                d.put("name", tabfix + SystemEnv.getHtmlLabelName(648, user.getLanguage()) + "ID");
                                d.put("fieldnamefix", tabfix + "requestid");
                                data.add(d);
                            }
                            Map<String, String> d = new HashMap<String, String>();
                            d.put("fieldname", fieldname);
                            d.put("fieldlabel", fieldlabel);
                            d.put("tablename", tablename);
                            d.put("tablelabel", tablelable);
                            d.put("tabfix", tabfix);
                            d.put("name", tabfix + fieldlabel);
                            d.put("fieldnamefix", tabfix + fieldname);
                            data.add(d);
                        }
                    }
                    boolean dtfirstflag = true;
                    rs3.executeSql("select id,namelabel from workflow_bill where detailtablename='" + tablename + "'");// 单据明细字段1
                    rs.executeSql("select tablename from Workflow_billdetailtable where tablename='" + tablename + "'");
                    if (rs3.next() && !rs.next()) {

                        dtfirstflag = true;
                        String billid = rs3.getString("id");
                        tablelable = SystemEnv.getHtmlLabelName(rs3.getInt("namelabel"), user.getLanguage())
                                + "(" + SystemEnv.getHtmlLabelName(19325, user.getLanguage()) + "1)";
                        rs.executeSql("select fieldname,fieldlabel from workflow_billfield where viewtype=1 and billid=" + billid);
                        while (rs.next()) {
                            String fieldname = rs.getString("fieldname");
                            String fieldlabel = SystemEnv.getHtmlLabelName(rs.getInt("fieldlabel"), user.getLanguage());
                            if (!searchfieldname.equals("") && fieldlabel.indexOf(searchfieldname) < 0)
                                continue;

                            if (dtfirstflag && searchfieldname.equals("")) {
                                dtfirstflag = false;

                                Map<String, String> d = new HashMap<String, String>();
                                d.put("fieldname", "ID");
                                d.put("fieldlabel", SystemEnv.getHtmlLabelName(81287, Util.getIntValue("" + user.getLanguage(), 7)));
                                d.put("tablename", tablename);
                                d.put("tablelabel", tablelable);
                                d.put("tabfix", tabfix);
                                d.put("name", tabfix + SystemEnv.getHtmlLabelName(81287, Util.getIntValue("" + user.getLanguage(), 7)));
                                d.put("fieldnamefix", tabfix + "ID");

                                data.add(d);
                            }

                            Map<String, String> d = new HashMap<String, String>();
                            d.put("fieldname", fieldname);
                            d.put("fieldlabel", fieldlabel);
                            d.put("tablename", tablename);
                            d.put("tablelabel", tablelable);
                            d.put("tabfix", tabfix);
                            d.put("name", tabfix + fieldlabel);
                            d.put("fieldnamefix", tabfix + fieldname);


                            data.add(d);
                        }
                    }
                    rs3.executeSql("select billid from Workflow_billdetailtable where tablename='" + tablename + "'");// 新表单,单据明细字段2
                    if (rs3.next()) {
                        String billid = rs3.getString("billid");
                        rs3.executeSql("select namelabel from workflow_bill where id=" + billid);
                        if (rs3.next())
                            tablelable = SystemEnv.getHtmlLabelName(rs3.getInt("namelabel"), user.getLanguage());

                        rs3.executeSql("select tablename from Workflow_billdetailtable where billid="
                                + billid + " order by orderid");
                        int detailIndex = 0;
                        while (rs3.next()) {
                            detailIndex++;
                            String detailtablename = Util.null2String(rs3.getString("tablename"));
                            if (detailtablename.equals(tablename)) {
                                tablelable = tablelable + "(" + SystemEnv.getHtmlLabelName(19325, user.getLanguage()) + detailIndex + ")";
                            }
                        }

                        String mainid = "";
                        rs.executeQuery("select detailkeyfield from workflow_bill where id = "+billid);
                        while (rs.next()){
                            mainid = Util.null2String(rs.getString(1));
                        }

                        dtfirstflag = true;
                        rs.executeSql("select fieldname,fieldlabel from workflow_billfield where viewtype=1 and detailtable='"
                                + tablename + "' and billid=" + billid);
                        while (rs.next()) {
                            String fieldname = rs.getString("fieldname");
                            String fieldlabel = SystemEnv.getHtmlLabelName(rs.getInt("fieldlabel"), user.getLanguage());
                            if (!searchfieldname.equals("") && fieldlabel.indexOf(searchfieldname) < 0)
                                continue;

                            if (dtfirstflag && searchfieldname.equals("")) {
                                dtfirstflag = false;

                                Map<String, String> d = new HashMap<String, String>();
                                d.put("fieldname", "ID");
                                d.put("fieldlabel", SystemEnv.getHtmlLabelName(81287, Util.getIntValue("" + user.getLanguage(), 7)));
                                d.put("tablename", tablename);
                                d.put("tablelabel", tablelable);
                                d.put("tabfix", tabfix);
                                d.put("name", tabfix + SystemEnv.getHtmlLabelName(81287, Util.getIntValue("" + user.getLanguage(), 7)));
                                d.put("fieldnamefix", tabfix + "ID");
                                data.add(d);

                                if ("mainid".equals(mainid)){
                                    HashMap<String, String> d1 = new HashMap<>();
                                    d1.put("fieldname", mainid);
                                    d1.put("fieldlabel", mainid);
                                    d1.put("tablename", tablename);
                                    d1.put("tablelabel", tablelable);
                                    d1.put("tabfix", tabfix);
                                    d1.put("name", tabfix + mainid);
                                    d1.put("fieldnamefix", tabfix + mainid);
                                    data.add(d1);
                                }

                            }

                            Map<String, String> d = new HashMap<String, String>();
                            d.put("fieldname", fieldname);
                            d.put("fieldlabel", fieldlabel);
                            d.put("tablename", tablename);
                            d.put("tablelabel", tablelable);
                            d.put("tabfix", tabfix);
                            d.put("name", tabfix + fieldlabel);
                            d.put("fieldnamefix", tabfix + fieldname);
                            data.add(d);
                        }

                        String rgx = SystemEnv.getHtmlLabelName(503443, Util.getIntValue("" + user.getLanguage(), 7));
                        if (!searchfieldname.equals("") && rgx.contains(searchfieldname)) {
                            HashMap<String, String> d1 = new HashMap<>();
                            d1.put("fieldname", mainid);
                            d1.put("fieldlabel", SystemEnv.getHtmlLabelName(503443, Util.getIntValue("" + user.getLanguage(), 7)));
                            d1.put("tablename", tablename);
                            d1.put("tablelabel", tablelable);
                            d1.put("tabfix", tabfix);
                            d1.put("name", tabfix + SystemEnv.getHtmlLabelName(503443, Util.getIntValue("" + user.getLanguage(), 7)));
                            d1.put("fieldnamefix", tabfix + mainid);
                            data.add(d1);
                        }
                    }
                    rs3.executeSql("select id,tabledesc,tabledescen from Sys_tabledict where tablename='"
                            + tablename + "'");// 其它模块
                    if (rs3.next()) {
                        String tabledictid = rs3.getString("id");
                        if (user.getLanguage() == 7 || user.getLanguage() == 9) {
                            tablelable = rs3.getString("tabledesc");
                        }else {
                            tablelable = rs3.getString("tabledescen");
                        }
                        List<String> allColumns = getAllColumns(tablename);
                        tablelable += "(" + SystemEnv.getHtmlLabelName(21778, user.getLanguage()) + ")";
                        rs.executeSql("select fieldname,fielddesc,fielddescen from Sys_fielddict where tabledictid="
                                + tabledictid + " order by dsporder");
                        while (rs.next()) {
                            String fieldname = rs.getString("fieldname").toLowerCase();
                            if(allColumns.size() > 0 && allColumns.contains(fieldname)){
                                allColumns.remove(fieldname);
                            }
                            String fieldlabel = "";
                            if (user.getLanguage() == 7 || user.getLanguage() == 9) {
                                fieldlabel = rs.getString("fielddesc");
                            }else {
                                fieldlabel = rs.getString("fielddescen");
                            }
                            if (!searchfieldname.equals("") && fieldlabel.indexOf(searchfieldname) < 0)
                                continue;
                            Map<String, String> d = new HashMap<String, String>();
                            d.put("fieldname", fieldname);
                            d.put("fieldlabel", fieldlabel);
                            d.put("tablename", tablename);
                            d.put("tablelabel", tablelable);
                            d.put("tabfix", tabfix);
                            d.put("name", tabfix + fieldlabel);
                            d.put("fieldnamefix", tabfix + fieldname);
                            data.add(d);
                        }


                        for (String column : allColumns) {
                            Map<String, String> d = new HashMap<String, String>();
                            if (!searchfieldname.equals("") && column.indexOf(searchfieldname) < 0)
                                continue;
                            d.put("fieldname", column);
                            d.put("fieldlabel", column);
                            d.put("tablename", tablename);
                            d.put("tablelabel", tablelable);
                            if (diyname.equals("-")) {
                                tabfix = tablename + ".";
                            } else {
                                tabfix = diyname + ".";
                            }
                            d.put("tabfix", tabfix);
                            d.put("name", tabfix + column);

                            d.put("fieldnamefix", tabfix + column);
                            data.add(d);
                        }
                    }
                }
            }
        }
        for (Map<String, String> datum : data) {
            if ("".equals(datum.get("tabfix"))) {
                datum.put("name", datum.get("tablelabel") + "." + datum.get("fieldlabel"));
            }
        }
        return data;
    }


    /**
     * 取得表的所有字段名称
     * @param tablename
     * @return
     */
    public List<String> getAllColumns(String tablename) {
        RecordSet rs = new RecordSet();
        String dbtype = rs.getDBType();
        List<String> returnArr = new ArrayList();
        tablename = Util.null2String(tablename);
        if (tablename.equals("")) return returnArr;
        String sql = "";

        if("sqlserver".equalsIgnoreCase(dbtype)){
            sql = "select c.name from sysobjects o,syscolumns c where o.id=c.id and o.name='" + tablename + "' order by c.colid";
        } else if("mysql".equalsIgnoreCase(dbtype)){
            sql = "select COLUMN_NAME from information_schema.columns where table_name='" + tablename + "'  and table_schema=database() ";
        }
        else if("postgresql".equalsIgnoreCase(dbtype)){
            sql = "select COLUMN_NAME from information_schema.columns where table_name=lower('" + tablename + "')  ";
        }
        else {
            sql = "select COLUMN_NAME from user_tab_columns where table_name=upper('" + tablename + "') ORDER BY COLUMN_ID";
        }
        rs.executeQuery(sql);
        while (rs.next()) {
            String columnname = Util.null2String(rs.getString(1)).toLowerCase();
            if("uuid".equals(columnname) || columnname.indexOf("password") > -1){
                continue;
            }
            returnArr.add(columnname);
        }
        return returnArr;
    }

    /**
     *
     * 根据orderid排序，重新计算明细表groupid，防止删除明细表后导致明细表和groupid对不上
     *
     * @param isbill
     * @param formid
     * @param rs
     * @return
     */
    public Map<String,Integer> getGroupIdMap(String isbill, String formid,RecordSet rs){
        Map<String,Integer> groupIdMap =new HashMap<>();
        String sqlForGroupid = "";
        int groupid = 1;
        if(isbill.equals("0")){
            sqlForGroupid = "select distinct groupid as tablename from workflow_formfield where formid="+formid+" and isdetail='1' order by groupid";
        }else {
            sqlForGroupid = "select tablename from workflow_billdetailtable where billid="+formid+" order by orderid";
        }
        rs.executeSql(sqlForGroupid);
        while (rs.next()) {
            String tablename = rs.getString("tablename");
            groupIdMap.put(tablename,groupid);
            groupid++;
        }
        return groupIdMap;
    }



}